#include"Queue.h"
#include"tree.h"
void QueueInit(Queue* q)
{
	assert(q);
	q->head = q->tail = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->data = data;
	newnode->next = NULL;
	if (q->head == NULL)
	{
		q->head = q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = q->tail->next;
	}
}

void QueuePop(Queue* q)
{
	assert(q);
	if (q->head == q->tail)
	{
		free(q->head);
		q->head = q->tail = NULL;
	}
	else
	{
		QNode* next = q->head->next;
		free(q->head);
		q->head = next;
	} 
}

QDataType QueueFront(Queue* q)
{
	return q->head->data;
}

QDataType QueueBack(Queue* q)
{
	return q->tail->data;
}

int QueueSize(Queue* q)
{
	int cout = 0;
	QNode* tmp = q->head;
	while (tmp != NULL)
	{
		cout++;
		tmp = tmp->next;
	}
	return cout;
}

int QueueEmpty(Queue* q)
{
	if (q->head == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* pcur = q->head;
	while (pcur)
	{
		QNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	q->head = q->tail = NULL;
}